package org.jeecg.modules.FinanceModule.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.FinanceModule.entity.FabricDzEntity;
import org.jeecg.modules.FinanceModule.entity.InvoiceEntity;
import org.jeecg.modules.FinanceModule.entity.ReconciliationEntity;
import org.jeecg.modules.FinanceModule.entity.SupplierEntity;
import org.jeecg.modules.FinanceModule.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Supplier")
public class SupplierController {
    @Autowired
    SupplierService service;
    /*
    查询供应商列表
     */
    @GetMapping("/list")
    public Result<?> SupplierList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String companyName,
            String id,
            String workNo
    ) {
        Page<SupplierEntity> page = new Page<SupplierEntity>(pageNo, pageSize);
        IPage<SupplierEntity> pageList = service.getSupplierList(page,companyName,id,workNo);
        return Result.ok(pageList);
    }

     /*
     查询生成的对账单供应商列表
      */
     @GetMapping("/temporaryduizhangSupplierList")
     public Result<?> temporaryduizhangSupplierList(
             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
             String[] ids
     ){
         if(ids==null||ids.length==0){
             return Result.ok("未选择对象");
         }
         Page<SupplierEntity> page = new Page<SupplierEntity>(pageNo, pageSize);
         IPage<SupplierEntity> pageList = service.temporaryduizhangSupplierList(page,ids);
         return Result.ok(pageList);
     }

    /*
    查询已有对账记录的供应商列表，放在后台供应商列表页面上
     */
    @GetMapping("/getyiReconciliationSupplierList")
    public Result<?> getyiReconciliationSupplierList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String companyName,
            String id,
            String starttime,
            String endtime
    ){
        Page<SupplierEntity> page = new Page<SupplierEntity>(pageNo, pageSize);
        IPage<SupplierEntity> pageList = service.getyiReconciliationSupplierList(page,companyName,id,starttime,endtime);
        return Result.ok(pageList);
    }
    /*
     查询供应商对账列表
     */
    @GetMapping("/duizhangList")
    public Result<?> duizhangList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "id") String SupplierId,
            String starttime,
            String endtime
    ) {
          if(starttime!=null&&starttime.equals("undefined"))
              starttime=null;
        if(endtime!=null&&endtime.equals("undefined"))
            endtime=null;
        Page<ReconciliationEntity> page = new Page<ReconciliationEntity>(pageNo, pageSize);
        IPage<SupplierEntity> pageList = service.getduizhangList(page, SupplierId,starttime,endtime);
        return Result.ok(pageList);
    }

    /*
    供应商对账列表上方的详情
     */
    @GetMapping("/duizhangdetail")
    public Result<?> duizhangdetail(
            @RequestParam(name = "id") String SupplierId
    ) {
        SupplierEntity entity = service.getduizhangdetail(SupplierId);
        return Result.ok(entity);
    }

    /*
    供应商开票记录列表查询
     */
    @GetMapping("/SupplierkaipiaoList")
    public Result<?> SupplierkaipiaoList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "id") String SupplierId
    ) {
        Page<InvoiceEntity> page = new Page<InvoiceEntity>(pageNo, pageSize);
        IPage<InvoiceEntity> pageList = service.getSupplierkaipiaoList(page, SupplierId);
        return Result.ok(pageList);
    }

   /*
    选择供应商 让他们这段时间内的对账同意
    */
    @PostMapping("approveoftimeduizhang")
    public Result<?> approveoftimeduizhang(
            @RequestBody HashMap<String,Object> map
           ){
        //将传过来的表单分别赋值一下
        List<String> ids= (List<String>) map.get("ids");
        String starttime= (String) map.get("starttime");
        String endtime= (String) map.get("endtime");
        if ("".equals(starttime)||starttime==null)
            starttime = "1949-10-01";
        if ("".equals(endtime)||endtime==null)
            endtime = "2099-12-20";


        System.out.println(starttime+"让我看一看开始时间和结束时间"+endtime);

        if(ids==null){
            return  Result.error("未选择供应商,请选择");
        }
        try {
            service.duizhangWork(ids,starttime,endtime);
            //将这些供应商的这时间内的全部对账全部通过
//            if(ids!=null&&ids.size()>0)
//            service.approveoftimeduizhang(ids,starttime,endtime);
            return Result.ok("成功了");
        }catch (Exception e){
            e.printStackTrace();
            return  Result.error("出错了");
        }
    }
    /*
    修改结算金额
     */
  @PostMapping("/updatesettlementMoney")
  public Result<?> updatesettlementMoney(
          @RequestBody     ReconciliationEntity entity
  ){
      int count=service.updatesettlementMoney(entity);
      return Result.ok("成功了");
  }
    /**
     * 查询这个供应商所有派单的产品的面料
     */
    @GetMapping("/getSysuserAllOrderFabric")
    public Result<?> getSysuserAllOrderFabric(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String sysuserId,
            String starttime,
            String endtime
    ){
        System.out.println(starttime+"输出看一看喽"+endtime);
           Page<FabricDzEntity> page=new  Page<FabricDzEntity>(pageNo,pageSize);
           IPage<FabricDzEntity> pageList=service.getSysuserAllOrderFabric(page,sysuserId,starttime,endtime);
           return Result.ok(pageList);
    }
    /*
    修改产品结算金额
     */
    @PostMapping("/updateProdsettleMoney")
    public Result<?> updateProdsettleMoney(
      @RequestBody HashMap<String,String> map
    ){
        int count=service.updateProdsettleMoney(map.get("id"),map.get("fabricSettleMoney"),map.get("type"));
        if(count>0){
            return  Result.ok("修改成功");
        }else {
            return  Result.error("修改失败");
        }
    }

    /**
     * 查询这个供应商所有的订单品类成本
     */
    @GetMapping("/getSysuserPingleicost")
    public Result<?> getSysuserPingleicost(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String sysuserId,
            String starttime,
            String endtime
    ){
        Page<FabricDzEntity> page=new  Page<FabricDzEntity>(pageNo,pageSize);
        IPage<FabricDzEntity> pageList=service.getSysuserPingleicost(page,sysuserId,starttime,endtime);
        return Result.ok(pageList);
    }

    /**
     * 查询这个供应商下的售价工艺对账表
     */
    @GetMapping("/getSysuserpaidangongyi")
    public Result<?> getSysuserpaidangongyi(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String sysuserId,
            String starttime,
            String endtime
    ){
        Page<FabricDzEntity> page=new  Page<FabricDzEntity>(pageNo,pageSize);
        IPage<FabricDzEntity> pageList=service.getSysuserpaidangongyi(page,sysuserId,starttime,endtime);
        return Result.ok(pageList);
    }

    /**
     * 查询这个供应商下的返修
     */
    @GetMapping("/getSysuserpaidanrepair")
    public Result<?> getSysuserpaidanrepair(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String sysuserId,
            String starttime,
            String endtime
    ){
        Page<FabricDzEntity> page=new  Page<FabricDzEntity>(pageNo,pageSize);
        IPage<FabricDzEntity> pageList=service.getSysuserpaidanrepair(page,sysuserId,starttime,endtime);
        return Result.ok(pageList);
    }
}
